package com.my.ribbon.support;

import com.alibaba.cloud.nacos.ribbon.NacosServer;
import com.my.ribbon.rule.DiscoveryEnabledRule;
import com.my.ribbon.rule.MetadataAwareRule;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * @author timo 2021/3/23
 */
@Configuration
@ConditionalOnClass({NacosServer.class})
@AutoConfigureBefore({RibbonClientConfiguration.class})
@EnableConfigurationProperties({CustomRibbonRuleProperties.class})
@ConditionalOnProperty(
        value = {"custom.ribbon.rule.enabled"},
        havingValue = "true"
)
public class CustomRibbonRuleAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    @Scope("prototype")
    public DiscoveryEnabledRule metadataAwareRule() {
        return new MetadataAwareRule();
    }

}
